<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="数据库-迁移" tabindex="-1"><a class="header-anchor" href="#数据库-迁移"><span>数据库: 迁移</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#generating-migrations">生成迁移</a>
<ul>
<li><a href="#squashing-migrations">整合迁移</a></li>
</ul>
</li>
<li><a href="#migration-structure">迁移结构</a></li>
<li><a href="#running-migrations">执行迁移</a>
<ul>
<li><a href="#rolling-back-migrations">回滚迁移</a></li>
</ul>
</li>
<li><a href="#tables">数据表</a>
<ul>
<li><a href="#creating-tables">创建数据表</a></li>
<li><a href="#updating-tables">更新数据表</a></li>
<li><a href="#renaming-and-dropping-tables">重命名 / 删除数据表</a></li>
</ul>
</li>
<li><a href="#columns">字段</a>
<ul>
<li><a href="#creating-columns">创建字段</a></li>
<li><a href="#available-column-types">可用的字段类型</a></li>
<li><a href="#column-modifiers">字段修饰符</a></li>
<li><a href="#modifying-columns">修改字段</a></li>
<li><a href="#renaming-columns">重命名字段</a></li>
<li><a href="#dropping-columns">删除字段</a></li>
</ul>
</li>
<li><a href="#indexes">索引</a>
<ul>
<li><a href="#creating-indexes">创建索引</a></li>
<li><a href="#renaming-indexes">重命名索引</a></li>
<li><a href="#dropping-indexes">删除索引</a></li>
<li><a href="#foreign-key-constraints">外键约束</a></li>
</ul>
</li>
<li><a href="#events">事件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>迁移就像数据库的版本控制，允许你的团队定义和共享应用程序的数据库架构定义。 如果你曾经不得不告诉团队成员在从代码控制中拉取更新后手动添加字段到他们的本地数据库，那么你就遇到了数据库迁移解决的问题。</p>
<p>Laravel 的 <code v-pre>Schema</code> <a href="https://learnku.com/docs/laravel/11.x/facades" target="_blank" rel="noopener noreferrer">facade</a> 为所有 Laravel 支持的数据库系统的创建和操作表提供了不依赖于数据库的支持。通常情况下，迁移会使用 facade 来创建和修改数据表和字段。</p>
<h2 id="生成迁移" tabindex="-1"><a class="header-anchor" href="#生成迁移"><span>生成迁移</span></a></h2>
<p>你可以使用<code v-pre>make:migration</code> <a href="https://learnku.com/docs/laravel/11.x/artisan" target="_blank" rel="noopener noreferrer">Artisan command</a> 来生成数据库迁移。新的迁移文件将放在你的 <code v-pre>database/migrations</code> 目录下。每个迁移文件名都包含一个时间戳来使 Laravel 确定迁移的顺序：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:migration create_flights_table</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 将使用迁移文件的名称来猜测表名以及迁移是否会创建一个新表。如果 Laravel 能够从迁移文件的名称中确定表的名称，它将在生成的迁移文件中预填入指定的表，或者，你也可以直接在迁移文件中手动指定表名。</p>
<p>如果你想在生成的迁移时指定自定义路径，你可以在执行 <code v-pre>make:migration</code> 命令时使用 <code v-pre>--path</code> 选项。给定的路径应该相对于应用程序的基本路径。</p>
<blockquote>
<p>!注意<br>
迁移时可以使用 <a href="https://learnku.com/docs/laravel/11.x/artisan#stub-customization" target="_blank" rel="noopener noreferrer">stub publishing</a> 自定义发布。</p>
</blockquote>
<h3 id="整合迁移" tabindex="-1"><a class="header-anchor" href="#整合迁移"><span>整合迁移</span></a></h3>
<p>在构建应用程序时，可能会随着时间的推移积累越来越多的迁移。这可能会导致你的 <code v-pre>database/migrations</code> 目录因为数百次迁移而变得臃肿。你如果愿意的话，可以将迁移「压缩」到单个 SQL 文件中。如果你想这样做，请先执行<code v-pre>schema:dump</code> 命令:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan schema:dump</span>
<span class="line"></span>
<span class="line"><span class="token comment"># 转存当前数据库架构并删除所有现有迁移...</span></span>
<span class="line">php artisan schema:dump <span class="token parameter variable">--prune</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>执行此命令时，Laravel 将向应用程序的 <code v-pre>database/schema</code> 目录写入一个「schema」文件。现在，当你尝试迁移数据库而没有执行其他迁移时，Laravel 将首先执行模式文件的 SQL 语句。在执行数据库结构文件的语句之后，Laravel 将执行不属于数据库结构的剩余的所有迁移。</p>
<p>如果你的应用程序的单元测试使用的数据库连接与你在本地开发过程中使用的不同，你应该确保你已经使用该数据库连接转存了一个 「schema」 文件，以便你的单元测试能够建立你的数据库。在进行本地开发的时候，你可能希望在数据库执行「schema」 文件之后再执行此操作：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan schema:dump</span>
<span class="line">php artisan schema:dump <span class="token parameter variable">--database</span><span class="token operator">=</span>testing <span class="token parameter variable">--prune</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>你应该将数据库模式文件提交给源代码管理，以便团队中的其他新开发人员可以快速创建应用程序的初始数据库结构。</p>
<blockquote>
<p><strong>注意</strong><br>
整合迁移仅适用于 MySQL、PostgreSQL 和 SQLite 数据库，并使用数据库命令行的客户端。另外，数据库结构不能还原到内存中的 SQLite 数据库。</p>
</blockquote>
<h2 id="迁移结构" tabindex="-1"><a class="header-anchor" href="#迁移结构"><span>迁移结构</span></a></h2>
<p>迁移类包含两个方法：<code v-pre>up</code> 和 <code v-pre>down</code> 。<code v-pre>up</code> 方法用于向数据库中添加新表、列或索引，而 <code v-pre>down</code> 方法用于撤销 <code v-pre>up</code> 方法执行的操作。</p>
<p>在这两种方法中，可以使用 Laravel 模式构建器来富有表现力地创建和修改表。要了解 <code v-pre>Schema</code> 构建器上可用的所有方法，<a href="#creating-tables">查看文档</a>。例如，以下迁移会创建一个 <code v-pre>flights</code> 表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Migrations<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token keyword">class</span> <span class="token class-name-definition class-name">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 执行迁移</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flights'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'airline'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * </span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">drop</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flights'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="设置迁移连接" tabindex="-1"><a class="header-anchor" href="#设置迁移连接"><span>设置迁移连接</span></a></h4>
<p>如果你的迁移将与应用程序默认数据库连接以外的数据库连接进行交互，你应该设置迁移的 <code v-pre>$connection</code> 属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指定数据库连接进行迁移</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'pgsql'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 执行迁移</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="执行迁移" tabindex="-1"><a class="header-anchor" href="#执行迁移"><span>执行迁移</span></a></h2>
<p>执行 <code v-pre>Artisan</code> 的 <code v-pre>migrate</code> 命令，来运行所有未执行过的迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想查看目前已经执行了哪些迁移，可以使用 Artisan 的 <code v-pre>migrate:status</code> 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:status</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你希望在不实际运行迁移的情况下看到将被执行的 SQL 语句，你可以在 <code v-pre>migrate</code> 命令中提供 <code v-pre>--pretend</code> 选项。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate <span class="token parameter variable">--pretend</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="在隔离的环境中执行迁移" tabindex="-1"><a class="header-anchor" href="#在隔离的环境中执行迁移"><span>在隔离的环境中执行迁移</span></a></h4>
<p>如果你在多个服务器上部署你的应用程序，并在部署过程中运行迁移，你可能不希望两个服务器同时尝试迁移数据库。为了避免这种情况，你可以在调用 <code v-pre>migrate</code> 命令时使用 <code v-pre>isolated</code> 选项。</p>
<p>当提供 <code v-pre>isolated</code> 选项时，Laravel 将使用你的应用程序缓存驱动获得一个原子锁，然后再尝试运行你的迁移。所有其他试图运行 <code v-pre>migrate</code> 命令的尝试在锁被持有时都不会执行；然而，命令仍然会以成功的退出状态码退出:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate <span class="token parameter variable">--isolated</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
要使用这个功能，你的应用程序必须使用 <code v-pre>memcached</code> / <code v-pre>redis</code> / <code v-pre>dynamodb</code> / <code v-pre>database</code> / <code v-pre>file</code> 或 <code v-pre>array</code> 缓存驱动作为你应用程序的默认缓存驱动。此外，所有的服务器必须与同一个中央缓存服务器进行通信。</p>
</blockquote>
<h4 id="在生产环境中执行强制迁移" tabindex="-1"><a class="header-anchor" href="#在生产环境中执行强制迁移"><span>在生产环境中执行强制迁移</span></a></h4>
<p>有些迁移操作是破坏性的，这意味着它们可能会导致数据丢失。为了防止你对生产数据库运行这些命令，在执行这些命令之前，系统将提示你进行确认。如果要在运行强制命令的时候去掉提示，需要加上 <code v-pre>--force</code> 标志：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate <span class="token parameter variable">--force</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="回滚迁移" tabindex="-1"><a class="header-anchor" href="#回滚迁移"><span>回滚迁移</span></a></h3>
<p>如果要回滚最后一次迁移操作，可以使用 Artisan <code v-pre>rollback</code> 命令。该命令会回滚最后「一批」的迁移，这可能包含多个迁移文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:rollback</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>通过向 <code v-pre>rollback</code> 命令加上 <code v-pre>step</code> 参数，可以回滚指定数量的迁移。例如，以下命令将回滚最后五个迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:rollback <span class="token parameter variable">--step</span><span class="token operator">=</span><span class="token number">5</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过向 <code v-pre>rollback</code> 命令提供 <code v-pre>batch</code> 选项来回滚特定的批次迁移，其中 <code v-pre>batch</code> 选项对应于应用程序中 <code v-pre>migrations</code> 数据库表中的一个批次值。例如，下面的命令将回滚第三批中的所有迁移。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"> php artisan migrate:rollback <span class="token parameter variable">--batch</span><span class="token operator">=</span><span class="token number">3</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你希望在不实际运行迁移的情况下看到将被执行的 SQL 语句，你可以添加<code v-pre>--pretend</code> 选项到 <code v-pre>migrate:rollback</code> 命令中:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:rollback <span class="token parameter variable">--pretend</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>migrate:reset</code> 命令会回滚应用已运行过的所有迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:reset</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="使用单个命令同时进行回滚和迁移操作" tabindex="-1"><a class="header-anchor" href="#使用单个命令同时进行回滚和迁移操作"><span>使用单个命令同时进行回滚和迁移操作</span></a></h4>
<p><code v-pre>migrate:refresh</code> 命令首先会回滚已运行过的所有迁移，随后会执行 <code v-pre>migrate</code>。这一命令可以高效地重建你的整个数据库：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:refresh</span>
<span class="line"></span>
<span class="line"><span class="token comment"># 重置数据库，并运行所有的 seeds...</span></span>
<span class="line">php artisan migrate:refresh <span class="token parameter variable">--seed</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通过在命令 <code v-pre>refresh</code> 中使用 <code v-pre>step</code> 参数，你可以回滚并重新执行指定数量的迁移操作。例如，下列命令会回滚并重新执行最后五个迁移操作：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:refresh <span class="token parameter variable">--step</span><span class="token operator">=</span><span class="token number">5</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="删除所有表然后执行迁移" tabindex="-1"><a class="header-anchor" href="#删除所有表然后执行迁移"><span>删除所有表然后执行迁移</span></a></h4>
<p><code v-pre>migrate:fresh</code> 命令会删去数据库中的所有表，随后执行命令 <code v-pre>migrate</code> ：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:fresh</span>
<span class="line"></span>
<span class="line">php artisan migrate:fresh <span class="token parameter variable">--seed</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，<code v-pre>migrate:fresh</code> 命令仅会删除默认数据库连接中的表。然而，你可以使用 <code v-pre>--database</code> 选项来指定应该迁移的数据库连接。数据库连接名称应为 <code v-pre>database</code> <a href="https://learnku.com/docs/laravel/11.x/configuration" target="_blank" rel="noopener noreferrer">配置文件</a> 中定义的连接。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:fresh <span class="token parameter variable">--database</span><span class="token operator">=</span>admin</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
<code v-pre>migrate:fresh</code> 命令在删去所有数据表的过程中，会无视它们的前缀。如果数据库涉及到其它应用，使用该命令须十分小心。</p>
</blockquote>
<h2 id="数据表" tabindex="-1"><a class="header-anchor" href="#数据表"><span>数据表</span></a></h2>
<h3 id="创建数据表" tabindex="-1"><a class="header-anchor" href="#创建数据表"><span>创建数据表</span></a></h3>
<p>接下来我们将使用 <code v-pre>Schema</code> 的 <code v-pre>create</code> 方法创建一个新的数据表。<code v-pre>create</code> 接受两个参数：第一个参数是表名，而第二个参数是一个闭包，该闭包接受一个用来定义新数据表的 <code v-pre>Blueprint</code> 对象：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>创建表时，可以使用数据库结构构建器的 <a href="#creating-columns">列方法</a> 来定义表的列。</p>
<h4 id="检查表-列是否存在" tabindex="-1"><a class="header-anchor" href="#检查表-列是否存在"><span>检查表 / 列是否存在</span></a></h4>
<p>你可以使用 <code v-pre>hasTable</code> 和 <code v-pre>hasColumn</code>, <code v-pre>hasIndex</code> 方法检查表或列或者索引是否存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">hasTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 「users」表存在...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">hasColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 「users」表存在，并且有「email」列...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">hasIndex</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'unique'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 「users」表存在，并且有「email」列有唯一索引...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="数据库连接和表选项" tabindex="-1"><a class="header-anchor" href="#数据库连接和表选项"><span>数据库连接和表选项</span></a></h4>
<p>如果要对不是应用程序默认的数据库连接执行数据库结构的操作，请使用 <code v-pre>connection</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">connection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sqlite'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，还可以使用其他一些属性和方法来定义表创建的其他地方。使用 MySQL 时，可以使用 <code v-pre>engine</code> 属性指定表的存储引擎：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">engine</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'InnoDB'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>charset</code> 和 <code v-pre>collation</code> 属性可用于在使用 MySQL 时为创建的表指定字符集和排序规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">charset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'utf8mb4'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">collation</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'utf8mb4_unicode_ci'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>temporary</code> 方法可用于将表标识为「临时」状态。临时表仅对当前连接的数据库会话可见，当连接关闭时会自动删除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'calculations'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">temporary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想给数据库表添加「注释」，你可以在表实例上调用 <code v-pre>comment</code> 方法。目前只有 MySQL 和 Postgres 支持表注释：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'calculations'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">comment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'业务计算'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="更新数据表" tabindex="-1"><a class="header-anchor" href="#更新数据表"><span>更新数据表</span></a></h3>
<p><code v-pre>Schema</code> facade 的 <code v-pre>table</code> 方法可用于更新现有表。与 <code v-pre>create</code> 方法一样，<code v-pre>table</code> 方法接受两个参数：表的名称和接收可用于向表添加列或索引的 <code v-pre>Blueprint</code> 实例的闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="重命名-删除表" tabindex="-1"><a class="header-anchor" href="#重命名-删除表"><span>重命名 / 删除表</span></a></h3>
<p>要重命名已存在的数据表，使用 <code v-pre>rename</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">rename</span><span class="token punctuation">(</span><span class="token variable">$from</span><span class="token punctuation">,</span> <span class="token variable">$to</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要删除已存在的表，你可以使用 <code v-pre>drop</code> 或 <code v-pre>dropIfExists</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">drop</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">dropIfExists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="使用外键重命名表" tabindex="-1"><a class="header-anchor" href="#使用外键重命名表"><span>使用外键重命名表</span></a></h4>
<p>在重命名表之前，应该确认表的所有外键约束在迁移文件中有一个显式的名称，而不是让 Laravel 去指定。否则，外键约束名称将引用旧表名。</p>
<h2 id="字段" tabindex="-1"><a class="header-anchor" href="#字段"><span>字段</span></a></h2>
<h3 id="创建字段" tabindex="-1"><a class="header-anchor" href="#创建字段"><span>创建字段</span></a></h3>
<p>门面 <code v-pre>Schema</code> 的 <code v-pre>table</code> 方法可用于更新表。与 <code v-pre>create</code> 方法一样， <code v-pre>table</code> 方法接受两个参数：表名和一个闭包，该闭包接收一个 <code v-pre>Illuminate\Database\Schema\Blueprint</code> 实例，可以使用该实例向表中添加列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="可用的字段类型" tabindex="-1"><a class="header-anchor" href="#可用的字段类型"><span>可用的字段类型</span></a></h3>
<p>Schema 构建器 <code v-pre>Illuminate\Database\Schema\Blueprint</code> 提供了多种方法，用来创建表中对应类型的列。下面列出了所有可用的方法：</p>
<h4 id="bigincrements" tabindex="-1"><a class="header-anchor" href="#bigincrements"><span><code v-pre>bigIncrements()</code></span></a></h4>
<p><code v-pre>bigIncrements</code> 方法创建一个自增的 UNSIGNED BIGINT（主键）等价列:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">bigIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="biginteger" tabindex="-1"><a class="header-anchor" href="#biginteger"><span><code v-pre>bigInteger()</code></span></a></h4>
<p><code v-pre>bigInteger</code> 方法创建一个 <code v-pre>BIGINT</code> 类型的等价列:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">bigInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="binary" tabindex="-1"><a class="header-anchor" href="#binary"><span><code v-pre>binary()</code></span></a></h4>
<p><code v-pre>binary</code> 方法创建一个 <code v-pre>BLOB</code> 类型的等价列:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">binary</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在使用MySQL、MariaDB或SQL Server时，你可以传参 <code v-pre>length</code> 和 <code v-pre>fixed</code> 来创建 <code v-pre>VARBINARY</code> 或 <code v-pre>BINARY</code> 类型的等价列:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">binary</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">,</span> <span class="token argument-name">length</span><span class="token punctuation">:</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// VARBINARY(16)</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">binary</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">,</span> <span class="token argument-name">length</span><span class="token punctuation">:</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token argument-name">fixed</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// BINARY(16)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="boolean" tabindex="-1"><a class="header-anchor" href="#boolean"><span><code v-pre>boolean()</code></span></a></h4>
<p><code v-pre>boolean</code> 方法创建一个 <code v-pre>BOOLEAN</code> 类型的等价列:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">boolean</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'confirmed'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="char" tabindex="-1"><a class="header-anchor" href="#char"><span><code v-pre>char()</code></span></a></h4>
<p><code v-pre>char</code> 方法创建一个给定长度的 <code v-pre>CHAR</code> 类型的等价列:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">char</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token argument-name">length</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="datetimetz" tabindex="-1"><a class="header-anchor" href="#datetimetz"><span><code v-pre>dateTimeTz()</code></span></a></h4>
<p><code v-pre>dateTimeTz</code> 方法创建一个带有时区信息的 <code v-pre>DATETIME</code> 类型的等价列，并可选择性地传参 <code v-pre>precision</code> 设置精度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dateTimeTz</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="datetime" tabindex="-1"><a class="header-anchor" href="#datetime"><span><code v-pre>dateTime()</code></span></a></h4>
<p><code v-pre>dateTime</code> 方法创建一个 <code v-pre>DATETIME</code> 类型的等价列，并可选择性地传参 <code v-pre>precision</code> 设置精度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dateTime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="date" tabindex="-1"><a class="header-anchor" href="#date"><span><code v-pre>date()</code></span></a></h4>
<p>The <code v-pre>date</code> method creates a <code v-pre>DATE</code> equivalent column:<br>
<code v-pre>date</code> 方法创建一个 <code v-pre>DATE</code> 类型的等价列。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">date</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="decimal" tabindex="-1"><a class="header-anchor" href="#decimal"><span><code v-pre>decimal()</code></span></a></h4>
<p><code v-pre>decimal</code> 方法创建一个 <code v-pre>DECIMAL</code> 类型的等价列，它具有指定的精度（总位数）和小数位数（小数点后的位数）</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">decimal</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'amount'</span><span class="token punctuation">,</span> <span class="token argument-name">total</span><span class="token punctuation">:</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token argument-name">places</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="double" tabindex="-1"><a class="header-anchor" href="#double"><span><code v-pre>double()</code></span></a></h4>
<p><code v-pre>double</code> 方法创建一个 <code v-pre>DOUBLE</code> 类型的等价列:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">double</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'amount'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="enum" tabindex="-1"><a class="header-anchor" href="#enum"><span><code v-pre>enum()</code></span></a></h4>
<p><code v-pre>enum</code> 方法创建一个具有给定有效值的 <code v-pre>ENUM</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">enum</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'difficulty'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'easy'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'hard'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="float" tabindex="-1"><a class="header-anchor" href="#float"><span><code v-pre>float()</code></span></a></h4>
<p><code v-pre>float</code> 方法创建一个具有给定精度的 <code v-pre>FLOAT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">float</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'amount'</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">53</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="foreignid" tabindex="-1"><a class="header-anchor" href="#foreignid"><span><code v-pre>foreignId()</code></span></a></h4>
<p><code v-pre>foreignId</code> 方法创建一个 <code v-pre>UNSIGNED BIGINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="foreignidfor" tabindex="-1"><a class="header-anchor" href="#foreignidfor"><span><code v-pre>foreignIdFor()</code></span></a></h4>
<p><code v-pre>foreignIdFor</code> 方法为给定的模型类添加一个 <code v-pre>{column}_id</code> 等效列。列类型将是 <code v-pre>UNSIGNED BIGINT</code>、<code v-pre>CHAR(36)</code> 或 <code v-pre>CHAR(26)</code>，这取决于模型键类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignIdFor</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="foreignulid" tabindex="-1"><a class="header-anchor" href="#foreignulid"><span><code v-pre>foreignUlid()</code></span></a></h4>
<p><code v-pre>foreignUlid</code> 方法创建一个 <code v-pre>ULID</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignUlid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="foreignuuid" tabindex="-1"><a class="header-anchor" href="#foreignuuid"><span><code v-pre>foreignUuid()</code></span></a></h4>
<p><code v-pre>foreignUuid</code> 方法创建一个 <code v-pre>UUID</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignUuid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="geography" tabindex="-1"><a class="header-anchor" href="#geography"><span><code v-pre>geography()</code></span></a></h4>
<p><code v-pre>geography</code> 方法创建一个具有给定空间类型和 SRID（空间参考系统标识符）的 <code v-pre>GEOGRAPHY</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">geography</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'coordinates'</span><span class="token punctuation">,</span> <span class="token argument-name">subtype</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'point'</span><span class="token punctuation">,</span> <span class="token argument-name">srid</span><span class="token punctuation">:</span> <span class="token number">4326</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
对于空间类型的支持取决于你的数据库驱动。请查阅你的数据库文档。如果你的应用使用 PostgreSQL 数据库，你必须在 <code v-pre>geography</code> 方法可用之前安装 <a href="https://postgis.net/" target="_blank" rel="noopener noreferrer">PostGIS</a> 扩展。</p>
</blockquote>
<h4 id="geometry" tabindex="-1"><a class="header-anchor" href="#geometry"><span><code v-pre>geometry()</code></span></a></h4>
<p><code v-pre>geometry</code> 方法创建一个具有给定空间类型和 SRID（空间参考系统标识符）的 <code v-pre>GEOMETRY</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">geometry</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'positions'</span><span class="token punctuation">,</span> <span class="token argument-name">subtype</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'point'</span><span class="token punctuation">,</span> <span class="token argument-name">srid</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
对于空间类型的支持取决于你的数据库驱动。请查阅你的数据库文档。如果你的应用使用 PostgreSQL 数据库，你必须在 <code v-pre>geometry</code> 方法可用之前安装 <a href="https://postgis.net/" target="_blank" rel="noopener noreferrer">PostGIS</a> 扩展。</p>
</blockquote>
<h4 id="id" tabindex="-1"><a class="header-anchor" href="#id"><span><code v-pre>id()</code></span></a></h4>
<p><code v-pre>id</code> 方法是 <code v-pre>bigIncrements</code> 方法的别名。默认情况下，该方法将创建一个 <code v-pre>id</code> 列；但是，如果你想要为该列分配一个不同的名称，可以传递一个列名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="increments" tabindex="-1"><a class="header-anchor" href="#increments"><span><code v-pre>increments()</code></span></a></h4>
<p><code v-pre>increments</code> 方法创建一个自动增长的 <code v-pre>UNSIGNED INTEGER</code> 等效列作为主键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">increments</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="integer" tabindex="-1"><a class="header-anchor" href="#integer"><span><code v-pre>integer()</code></span></a></h4>
<p><code v-pre>integer</code> 方法创建一个 <code v-pre>INTEGER</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="ipaddress" tabindex="-1"><a class="header-anchor" href="#ipaddress"><span><code v-pre>ipAddress()</code></span></a></h4>
<p><code v-pre>ipAddress</code> 方法创建一个 <code v-pre>VARCHAR</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">ipAddress</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'visitor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>使用 PostgreSQL 时，将创建一个 <code v-pre>INET</code> 列。</p>
<h4 id="json" tabindex="-1"><a class="header-anchor" href="#json"><span><code v-pre>json()</code></span></a></h4>
<p><code v-pre>json</code> 方法创建一个 <code v-pre>JSON</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="jsonb" tabindex="-1"><a class="header-anchor" href="#jsonb"><span><code v-pre>jsonb()</code></span></a></h4>
<p><code v-pre>jsonb</code> 方法创建一个 <code v-pre>JSONB</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">jsonb</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="longtext" tabindex="-1"><a class="header-anchor" href="#longtext"><span><code v-pre>longText()</code></span></a></h4>
<p><code v-pre>longText</code> 方法创建一个 <code v-pre>LONGTEXT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">longText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>使用 MySQL 或 MariaDB 时，你可以应用 <code v-pre>binary</code> 字符集到该列，以创建等同于 <code v-pre>LONGBLOB</code> 的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">longText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">charset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'binary'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// LONGBLOB</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="macaddress" tabindex="-1"><a class="header-anchor" href="#macaddress"><span><code v-pre>macAddress()</code></span></a></h4>
<p><code v-pre>macAddress</code> 方法创建一个旨在存储 MAC 地址的列。某些数据库系统（如 PostgreSQL）具有用于此类数据的专用列类型。其他数据库系统将使用等同于字符串的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">macAddress</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'device'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="mediumincrements" tabindex="-1"><a class="header-anchor" href="#mediumincrements"><span><code v-pre>mediumIncrements()</code></span></a></h4>
<p><code v-pre>mediumIncrements</code> 方法创建一个自增的等同于 <code v-pre>UNSIGNED MEDIUMINT</code> 的主键列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">mediumIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="mediuminteger" tabindex="-1"><a class="header-anchor" href="#mediuminteger"><span><code v-pre>mediumInteger()</code></span></a></h4>
<p><code v-pre>mediumInteger</code> 方法创建一个等同于 <code v-pre>MEDIUMINT</code> 的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">mediumInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="mediumtext" tabindex="-1"><a class="header-anchor" href="#mediumtext"><span><code v-pre>mediumText()</code></span></a></h4>
<p><code v-pre>mediumText</code> 方法创建一个等同于 <code v-pre>MEDIUMTEXT</code> 的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">mediumText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>使用 MySQL 或 MariaDB 时，你可以应用 <code v-pre>binary</code> 字符集到该列，以创建等同于 <code v-pre>MEDIUMBLOB</code> 的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">mediumText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">charset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'binary'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// MEDIUMBLOB</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="morphs" tabindex="-1"><a class="header-anchor" href="#morphs"><span><code v-pre>morphs()</code></span></a></h4>
<p><code v-pre>morphs</code> 方法是一个便利方法，添加一个 <code v-pre>{column}_id</code> 等同列和一个等同于 <code v-pre>VARCHAR</code> 的  <code v-pre>{column}_type</code> 的列。<code v-pre>{column}_id</code> 的列类型将根据模型键类型而定，可能是 <code v-pre>UNSIGNED BIGINT</code>、<code v-pre>CHAR(36)</code> 或 <code v-pre>CHAR(26)</code>。</p>
<p>这个方法旨在用于定义多态 <a href="https://learnku.com/docs/laravel/11.x/eloquent-relationshipsmd" target="_blank" rel="noopener noreferrer">Eloquent 关系</a> 所需的列。在以下示例中，将创建 <code v-pre>taggable_id</code> 和 <code v-pre>taggable_type</code> 列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">morphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="nullabletimestamps" tabindex="-1"><a class="header-anchor" href="#nullabletimestamps"><span><code v-pre>nullableTimestamps()</code></span></a></h4>
<p><code v-pre>nullableTimestamps</code> 方法是 <a href="#column-method-timestamps">timestamps</a>方法的别名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">nullableTimestamps</span><span class="token punctuation">(</span><span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="nullablemorphs" tabindex="-1"><a class="header-anchor" href="#nullablemorphs"><span><code v-pre>nullableMorphs()</code></span></a></h4>
<p>此方法类似于 <a href="#column-method-morphs">morphs</a> 方法；然而，创建的列将是「nullable」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">nullableMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="nullableulidmorphs" tabindex="-1"><a class="header-anchor" href="#nullableulidmorphs"><span><code v-pre>nullableUlidMorphs()</code></span></a></h4>
<p>此方法类似于 <a href="#column-method-ulidMorphs">ulidMorphs</a> 方法；然而，创建的列将是「nullable」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">nullableUlidMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="nullableuuidmorphs" tabindex="-1"><a class="header-anchor" href="#nullableuuidmorphs"><span><code v-pre>nullableUuidMorphs()</code></span></a></h4>
<p>此方法类似于 <a href="#column-method-uuidMorphs">uuidMorphs</a>方法；然而，创建的列将是「nullable」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">nullableUuidMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="remembertoken" tabindex="-1"><a class="header-anchor" href="#remembertoken"><span><code v-pre>rememberToken()</code></span></a></h4>
<p><code v-pre>rememberToken</code> 方法创建一个可为空的 <code v-pre>VARCHAR(100)</code> 等同列，用于存储当前的「记住我」<a href="https://learnku.com/docs/laravel/11.x/authenticationmd#remembering-users" target="_blank" rel="noopener noreferrer">认证令牌</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">rememberToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="set" tabindex="-1"><a class="header-anchor" href="#set"><span><code v-pre>set()</code></span></a></h4>
<p><code v-pre>set</code> 方法创建一个 <code v-pre>SET</code> 等同列，并附有给定的有效值列表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flavors'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'strawberry'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'vanilla'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="smallincrements" tabindex="-1"><a class="header-anchor" href="#smallincrements"><span><code v-pre>smallIncrements()</code></span></a></h4>
<p><code v-pre>smallIncrements</code> 方法创建一个自增的 <code v-pre>UNSIGNED SMALLINT</code> 等效列作为主键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">smallIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="smallinteger" tabindex="-1"><a class="header-anchor" href="#smallinteger"><span><code v-pre>smallInteger()</code></span></a></h4>
<p><code v-pre>smallInteger</code> 方法创建一个 <code v-pre>SMALLINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">smallInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="softdeletestz" tabindex="-1"><a class="header-anchor" href="#softdeletestz"><span><code v-pre>softDeletesTz()</code></span></a></h4>
<p><code v-pre>softDeletesTz</code> 方法添加了一个可以为空的 <code v-pre>deleted_at</code> <code v-pre>TIMESTAMP</code>（带有时区）等效的列，具有可选小数秒精度。此列旨在存储 Eloquent 「软删除」功能所需的 <code v-pre>deleted_at</code> 时间戳：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">softDeletesTz</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'deleted_at'</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="softdeletes" tabindex="-1"><a class="header-anchor" href="#softdeletes"><span><code v-pre>softDeletes()</code></span></a></h4>
<p><code v-pre>softDeletes</code> 方法添加了一个可以为空的 <code v-pre>deleted_at</code> <code v-pre>TIMESTAMP</code> 等效列，具有可选小数秒精度。此列旨在存储 Eloquent 「软删除」功能所需的 <code v-pre>deleted_at</code> 时间戳：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">softDeletes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'deleted_at'</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="string" tabindex="-1"><a class="header-anchor" href="#string"><span><code v-pre>string()</code></span></a></h4>
<p><code v-pre>string</code> 方法创建了一个给定长度的 <code v-pre>VARCHAR</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token argument-name">length</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="text" tabindex="-1"><a class="header-anchor" href="#text"><span><code v-pre>text()</code></span></a></h4>
<p><code v-pre>text</code> 方法创建了一个 <code v-pre>TEXT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在使用 MySQL 或 MariaDB 时，你可以将 <code v-pre>binary</code> 字符集应用于列，以创建一个 <code v-pre>BLOB</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">charset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'binary'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// BLOB</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timetz" tabindex="-1"><a class="header-anchor" href="#timetz"><span><code v-pre>timeTz()</code></span></a></h4>
<p><code v-pre>timeTz</code> 方法创建了一个带有可选小数秒精度的 <code v-pre>TIME</code>（带有时区）等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timeTz</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sunrise'</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="time" tabindex="-1"><a class="header-anchor" href="#time"><span><code v-pre>time()</code></span></a></h4>
<p><code v-pre>time</code> 方法创建了一个带有可选小数秒精度的 <code v-pre>TIME</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">time</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sunrise'</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timestamptz" tabindex="-1"><a class="header-anchor" href="#timestamptz"><span><code v-pre>timestampTz()</code></span></a></h4>
<p><code v-pre>timestampTz</code> 方法创建了一个带有可选小数秒精度的 <code v-pre>TIMESTAMP</code>（带有时区）等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestampTz</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'added_at'</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timestamp" tabindex="-1"><a class="header-anchor" href="#timestamp"><span><code v-pre>timestamp()</code></span></a></h4>
<p><code v-pre>timestamp</code> 方法创建了一个带有可选小数秒精度的 <code v-pre>TIMESTAMP</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamp</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'added_at'</span><span class="token punctuation">,</span> <span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timestampstz" tabindex="-1"><a class="header-anchor" href="#timestampstz"><span><code v-pre>timestampsTz()</code></span></a></h4>
<p><code v-pre>timestampsTz</code> 方法创建了带有可选小数秒精度 <code v-pre>created_at</code> 和 <code v-pre>updated_at</code> <code v-pre>TIMESTAMP</code>（带有时区）等效的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestampsTz</span><span class="token punctuation">(</span><span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timestamps" tabindex="-1"><a class="header-anchor" href="#timestamps"><span><code v-pre>timestamps()</code></span></a></h4>
<p><code v-pre>timestamps</code> 方法创建了带有可选小数秒精度 <code v-pre>created_at</code> 和 <code v-pre>updated_at</code> <code v-pre>TIMESTAMP</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token argument-name">precision</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="tinyincrements" tabindex="-1"><a class="header-anchor" href="#tinyincrements"><span><code v-pre>tinyIncrements()</code></span></a></h4>
<p><code v-pre>tinyIncrements</code> 方法创建了一个自增的 <code v-pre>UNSIGNED TINYINT</code> 等效列作为主键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">tinyIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="tinyinteger" tabindex="-1"><a class="header-anchor" href="#tinyinteger"><span><code v-pre>tinyInteger()</code></span></a></h4>
<p><code v-pre>tinyInteger</code> 方法创建了一个 <code v-pre>TINYINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">tinyInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="tinytext" tabindex="-1"><a class="header-anchor" href="#tinytext"><span><code v-pre>tinyText()</code></span></a></h4>
<p><code v-pre>tinyText</code> 方法创建了一个 <code v-pre>TINYTEXT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">tinyText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'notes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在使用 MySQL 或 MariaDB 时，你可以将 <code v-pre>binary</code> 字符集应用于列，以创建一个 <code v-pre>TINYBLOB</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">tinyText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">charset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'binary'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// TINYBLOB</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedbiginteger" tabindex="-1"><a class="header-anchor" href="#unsignedbiginteger"><span><code v-pre>unsignedBigInteger()</code></span></a></h4>
<p><code v-pre>unsignedBigInteger</code> 方法创建了一个 <code v-pre>UNSIGNED BIGINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedBigInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedinteger" tabindex="-1"><a class="header-anchor" href="#unsignedinteger"><span><code v-pre>unsignedInteger()</code></span></a></h4>
<p><code v-pre>unsignedInteger</code> 方法创建了一个 <code v-pre>UNSIGNED INTEGER</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedmediuminteger" tabindex="-1"><a class="header-anchor" href="#unsignedmediuminteger"><span><code v-pre>unsignedMediumInteger()</code></span></a></h4>
<p><code v-pre>unsignedMediumInteger</code> 方法创建了一个 <code v-pre>UNSIGNED MEDIUMINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedMediumInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedsmallinteger" tabindex="-1"><a class="header-anchor" href="#unsignedsmallinteger"><span><code v-pre>unsignedSmallInteger()</code></span></a></h4>
<p><code v-pre>unsignedSmallInteger</code> 方法创建了一个 <code v-pre>UNSIGNED SMALLINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedSmallInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedtinyinteger" tabindex="-1"><a class="header-anchor" href="#unsignedtinyinteger"><span><code v-pre>unsignedTinyInteger()</code></span></a></h4>
<p><code v-pre>unsignedTinyInteger</code> 方法创建了一个 <code v-pre>UNSIGNED TINYINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedTinyInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="ulidmorphs" tabindex="-1"><a class="header-anchor" href="#ulidmorphs"><span><code v-pre>ulidMorphs()</code></span></a></h4>
<p><code v-pre>ulidMorphs</code> 方法是一个便捷方法，添加一个 <code v-pre>{column}_id</code> <code v-pre>CHAR(26)</code> 等效列和一个 <code v-pre>{column}_type</code> <code v-pre>VARCHAR</code> 等效列。</p>
<p>此方法旨在定义使用 ULID 标识符的多态 <a href="https://learnku.com/docs/laravel/11.x/eloquent-relationshipsmd" target="_blank" rel="noopener noreferrer">Eloquent 关系</a>所需的列时使用。在以下示例中，将创建 <code v-pre>taggable_id</code> 和 <code v-pre>taggable_type</code> 列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">ulidMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="uuidmorphs" tabindex="-1"><a class="header-anchor" href="#uuidmorphs"><span><code v-pre>uuidMorphs()</code></span></a></h4>
<p><code v-pre>uuidMorphs</code> 方法是一个便捷方法，添加一个 <code v-pre>{column}_id</code> <code v-pre>CHAR(36)</code> 等效列和一个 <code v-pre>{column}_type</code> <code v-pre>VARCHAR</code> 等效列。</p>
<p>此方法旨在定义使用 UUID 标识符的多态 <a href="https://learnku.com/docs/laravel/11.x/eloquent-relationshipsmd" target="_blank" rel="noopener noreferrer">Eloquent 关系</a> 所需的列时使用。在以下示例中，将创建 <code v-pre>taggable_id</code> 和 <code v-pre>taggable_type</code> 列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">uuidMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="ulid" tabindex="-1"><a class="header-anchor" href="#ulid"><span><code v-pre>ulid()</code></span></a></h4>
<p><code v-pre>ulid</code> 方法创建一个 <code v-pre>ULID</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">ulid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="uuid" tabindex="-1"><a class="header-anchor" href="#uuid"><span><code v-pre>uuid()</code></span></a></h4>
<p><code v-pre>uuid</code> 方法创建一个 <code v-pre>UUID</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">uuid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="year" tabindex="-1"><a class="header-anchor" href="#year"><span><code v-pre>year()</code></span></a></h4>
<p><code v-pre>year</code> 方法创建一个 <code v-pre>YEAR</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">year</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'birth_year'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="列修饰符" tabindex="-1"><a class="header-anchor" href="#列修饰符"><span>列修饰符</span></a></h3>
<p>除了上面列出的列类型之外，当给数据库表添加一个列时，还有几个你可以使用的列「修饰符」。例如，要使列可以「为空」，你可以使用 <code v-pre>nullable</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">nullable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>以下表格包含所有可用的列修饰符。这个列表不包括 <a href="#creating-indexes">索引修饰符</a>：</p>
<table>
<thead>
<tr>
<th>修饰符</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>-&gt;after('column')</code></td>
<td>在另一列之后放置该列（仅限 MySQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;autoIncrement()</code></td>
<td>设置整数列为自动增长的（主键）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;charset('utf8mb4')</code></td>
<td>为列指定字符集（仅限 MySQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;collation('utf8mb4_unicode_ci')</code></td>
<td>为列指定排序规则。</td>
</tr>
<tr>
<td><code v-pre>-&gt;comment('my comment')</code></td>
<td>向列添加注释（MySQL / PostgreSQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;default($value)</code></td>
<td>为列指定「默认」值。</td>
</tr>
<tr>
<td><code v-pre>-&gt;first()</code></td>
<td>将列置于表的「第一列」（仅限 MySQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;from($integer)</code></td>
<td>设置自动增长字段的起始值（MySQL / PostgreSQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;invisible()</code></td>
<td>使列在 <code v-pre>SELECT *</code> 查询中「不可见」（仅限 MySQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;nullable($value = true)</code></td>
<td>允许插入 NULL 值到列中。</td>
</tr>
<tr>
<td><code v-pre>-&gt;storedAs($expression)</code></td>
<td>创建一个存储生成的列（MySQL / PostgreSQL / SQLite）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;unsigned()</code></td>
<td>设置整数列为 UNSIGNED（仅限 MySQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;useCurrent()</code></td>
<td>设置时间戳列使用 CURRENT_TIMESTAMP 作为默认值。</td>
</tr>
<tr>
<td><code v-pre>-&gt;useCurrentOnUpdate()</code></td>
<td>设置记录更新时，时间戳列使用 CURRENT_TIMESTAMP（仅限 MySQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;virtualAs($expression)</code></td>
<td>创建一个虚拟生成的列（MySQL / SQLite）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;generatedAs($expression)</code></td>
<td>使用指定的序列选项创建一个身份列（PostgreSQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;always()</code></td>
<td>定义身份列的序列值优先于输入（PostgreSQL）。</td>
</tr>
</tbody>
</table>
<h4 id="默认表达式" tabindex="-1"><a class="header-anchor" href="#默认表达式"><span>默认表达式</span></a></h4>
<p><code v-pre>default</code> 修饰符接受一个值或一个 <code v-pre>Illuminate\Database\Query\Expression</code> 实例。使用 <code v-pre>Expression</code> 实例将防止 Laravel 用引号包裹该值并允许你使用数据库特定的函数。当你需要为 JSON 列分配默认值时，这是特别有用的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Query<span class="token punctuation">\</span>Expression</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Migrations<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token keyword">class</span> <span class="token class-name-definition class-name">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     *  运行迁移。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flights'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'movies'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">default</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Expression</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'(JSON_ARRAY())'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
默认表达式的支持取决于你的数据库驱动程序、数据库版本和字段类型。请参阅你的数据库文档。</p>
</blockquote>
<h4 id="列顺序" tabindex="-1"><a class="header-anchor" href="#列顺序"><span>列顺序</span></a></h4>
<p>在使用 MySQL 数据库时，可以使用 <code v-pre>after</code> 方法在模式中的现有列之后添加列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'address_line1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'address_line2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'city'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="修改列" tabindex="-1"><a class="header-anchor" href="#修改列"><span>修改列</span></a></h3>
<p><code v-pre>change</code> 方法允许你修改现有列的类型和属性。例如，你可能希望增加 <code v-pre>string</code> 列的大小。要看到 <code v-pre>change</code> 方法的作用，让我们将 <code v-pre>name</code> 列的大小从 25 增加到 50。为此，我们只需定义列的新状态然后调用 <code v-pre>change</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">change</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>修改列时，必须明确包括你想要保留的所有修饰符在列定义上 - 任何缺失的属性都将被丢弃。例如，要保留 <code v-pre>unsigned</code>、<code v-pre>default</code> 和 <code v-pre>comment</code> 属性，你必须在更改列时明确调用每个修饰符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unsigned</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">default</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">comment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my comment'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">change</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>change</code> 方法不改变列的索引。因此，你可以使用索引修饰符在修改列时明确添加或删除索引：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 添加一个索引...</span></span>
<span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">bigIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">primary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">change</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 删除一个索引...</span></span>
<span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">char</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'postal_code'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">change</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="重命名列" tabindex="-1"><a class="header-anchor" href="#重命名列"><span>重命名列</span></a></h3>
<p>要重命名列，可以使用模式构建器提供的 <code v-pre>renameColumn</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">renameColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'to'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="删除列" tabindex="-1"><a class="header-anchor" href="#删除列"><span>删除列</span></a></h3>
<p>要删除列，可以在模式构建器上使用 <code v-pre>dropColumn</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以通过将列名数组传递给 <code v-pre>dropColumn</code> 方法，来从表中删除多个列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropColumn</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'location'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="可用的命令别名" tabindex="-1"><a class="header-anchor" href="#可用的命令别名"><span>可用的命令别名</span></a></h4>
<p>Laravel 提供了几个与删除常见类型列有关的方便方法。下表描述了每个方法：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;dropMorphs('morphable');</code></td>
<td>删除 <code v-pre>morphable_id</code> 和 <code v-pre>morphable_type</code> 列。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropRememberToken();</code></td>
<td>删除 <code v-pre>remember_token</code> 列。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropSoftDeletes();</code></td>
<td>删除 <code v-pre>deleted_at</code> 列。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropSoftDeletesTz();</code></td>
<td><code v-pre>dropSoftDeletes()</code> 方法的别名。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropTimestamps();</code></td>
<td>删除 <code v-pre>created_at</code> 和 <code v-pre>updated_at</code> 列。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropTimestampsTz();</code></td>
<td><code v-pre>dropTimestamps()</code> 方法的别名。</td>
</tr>
</tbody>
</table>
<h2 id="索引" tabindex="-1"><a class="header-anchor" href="#索引"><span>索引</span></a></h2>
<h3 id="创建索引" tabindex="-1"><a class="header-anchor" href="#创建索引"><span>创建索引</span></a></h3>
<p>Laravel 模式构建器支持几种索引类型。以下示例创建了一个新的 <code v-pre>email</code> 列，并指定其值应该是唯一的。要创建该索引，我们可以将 <code v-pre>unique</code> 方法链到列定义上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你也可以在定义列之后创建索引。要做到这一点，你应该在模式构建器蓝图上调用 <code v-pre>unique</code> 方法。这个方法接受一个唯一索引的列名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你甚至可以将数组传递给索引方法来创建一个复合（或合成）索引：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'account_id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>创建索引时，Laravel 会自动生成一个合理的索引名称，但你也可以传递第二个参数来自定义索引名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'unique_email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="可用的索引类型" tabindex="-1"><a class="header-anchor" href="#可用的索引类型"><span>可用的索引类型</span></a></h4>
<p>Laravel 的模式构建器提供了 Laravel 支持的所有类型的索引方法。每个索引方法都接受一个可选的第二个参数来指定索引的名称。如果省略，名称将根据表和列的名称生成。下面是所有可用的索引方法：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;primary('id');</code></td>
<td>添加一个主键。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;primary(['id', 'parent_id']);</code></td>
<td>添加复合主键。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;unique('email');</code></td>
<td>添加一个唯一索引。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;index('state');</code></td>
<td>添加一个基础索引。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;fullText('body');</code></td>
<td>添加一个全文索引（MySQL / PostgreSQL）。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;fullText('body')-&gt;language('english');</code></td>
<td>添加一个指定语言的全文索引（PostgreSQL）。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;spatialIndex('location');</code></td>
<td>添加一个空间索引（SQLite 除外）。</td>
</tr>
</tbody>
</table>
<h3 id="重命名索引" tabindex="-1"><a class="header-anchor" href="#重命名索引"><span>重命名索引</span></a></h3>
<p>要重命名索引，可以使用模式构建器提供的 <code v-pre>renameIndex</code> 方法。此方法接受当前索引名称作为其第一个参数，所需名称作为其第二个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">renameIndex</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'to'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="删除索引" tabindex="-1"><a class="header-anchor" href="#删除索引"><span>删除索引</span></a></h3>
<p>要删除索引，必须指定索引的名称。默认情况下，Laravel 会自动根据表名、索引的列名和索引类型分配索引名称。这里有一些例子：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;dropPrimary('users_id_primary');</code></td>
<td>从「users」表中删除主键</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropUnique('users_email_unique');</code></td>
<td>从「users」表中删除 unique 索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropIndex('geo_state_index');</code></td>
<td>从「geo」表中删除基本索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropFullText('posts_body_fulltext');</code></td>
<td>从「post」表中删除一个全文索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropSpatialIndex('geo_location_spatialindex');</code></td>
<td>从「geo」表中删除空间索引（不支持 SQLite）</td>
</tr>
</tbody>
</table>
<p>如果你传递一个列数组到删除索引的方法中，基于表名，列和索引类型的传统索引名称将被生成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'geo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropIndex</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'state'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Drops index 'geo_state_index'</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="外键约束" tabindex="-1"><a class="header-anchor" href="#外键约束"><span>外键约束</span></a></h3>
<p>Laravel 还支持创建用于在数据库层中的强制引用完整性的外键约束。例如，让我们在 <code v-pre>posts</code> 表上定义一个 <code v-pre>user_id</code> 列，它引用 <code v-pre>users</code> 表上的 <code v-pre>id</code> 列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedBigInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">references</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于这种外键约束的定义方式过于繁复，Laravel 额外提供了更简洁的方法，基于约定来提供更好的开发人员体验。当使用 <code v-pre>foreignId</code> 方法来创建列时，上面的示例还可以这么写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">constrained</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>foreignId</code> 方法创建了一个 <code v-pre>UNSIGNED BIGINT</code> 等效的列，而 <code v-pre>constrained</code> 方法将利用约定确定正在被引用的表和列。如果你的表名不符合 Laravel 的约定，你可以手动将其提供给 <code v-pre>constrained</code> 方法。此外，还可以指定生成的索引应该分配的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">constrained</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">table</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token argument-name">indexName</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'posts_user_id'</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以为约束的「on delete」和「on update」属性指定所需的操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">constrained</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">onUpdate</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cascade'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">onDelete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cascade'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>还为这些操作提供了另一种表达性语法：</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;cascadeOnUpdate();</code></td>
<td>更新应该级联</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;restrictOnUpdate();</code></td>
<td>更新应受限</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;noActionOnUpdate();</code></td>
<td>更新不执行任何动作</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;cascadeOnDelete();</code></td>
<td>删除应级联</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;restrictOnDelete();</code></td>
<td>删除应受限</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;nullOnDelete();</code></td>
<td>删除应将外键值设为 null</td>
</tr>
</tbody>
</table>
<p>当使用任意 <a href="#column-modifiers">列修饰符</a> 的时候，必须在调用 <code v-pre>constrained</code> 之前调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">nullable</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">constrained</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="删除外键" tabindex="-1"><a class="header-anchor" href="#删除外键"><span>删除外键</span></a></h4>
<p>要删除一个外键，你需要使用 <code v-pre>dropForeign</code> 方法，将要删除的外键约束作为参数传递。外键约束采用的命名方式与索引相同。即将数据表名称和约束的字段连接起来，再加上 <code v-pre>_foreign</code> 后缀：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropForeign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts_user_id_foreign'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，可以给 <code v-pre>dropForeign</code> 方法传递一个数组，该数组包含要删除的外键的列名。数组将根据 Laravel 的模式构建器使用的约束名称约定自动转换：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropForeign</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="更改外键约束" tabindex="-1"><a class="header-anchor" href="#更改外键约束"><span>更改外键约束</span></a></h4>
<p>你可以在迁移文件中使用以下方法来开启或关闭外键约束：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">enableForeignKeyConstraints</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">disableForeignKeyConstraints</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">withoutForeignKeyConstraints</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 闭包中禁用的约束…</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
SQLite 默认禁用外键约束。使用 SQLite 时，请确保在数据库配置中<a href="https://learnku.com/docs/laravel/11.x/databasemd#configuration" target="_blank" rel="noopener noreferrer">启用外键支持</a> 然后再尝试在迁移中创建它们。另外，SQLite 只在创建表时支持外键，并且<a href="https://www.sqlite.org/omitted.html" target="_blank" rel="noopener noreferrer">将在修改表时不会支持</a>。</p>
</blockquote>
<h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>为方便起见，每个迁移操作都会派发一个 <a href="https://learnku.com/docs/laravel/11.x/eventsmd" target="_blank" rel="noopener noreferrer">事件</a>。以下所有事件都扩展了基础 <code v-pre>Illuminate\Database\Events\MigrationEvent</code> 类：</p>
<table>
<thead>
<tr>
<th>类</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>Illuminate\Database\Events\MigrationsStarted</code></td>
<td>一批迁移即将执行</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\MigrationsEnded</code></td>
<td>一批迁移已经完成执行</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\MigrationStarted</code></td>
<td>一个单独的迁移即将执行</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\MigrationEnded</code></td>
<td>一个单独的迁移已经完成执行</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\NoPendingMigrations</code></td>
<td>没有发现等待迁移的迁移命令</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\SchemaDumped</code></td>
<td>数据库模式转储已完成</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\SchemaLoaded</code></td>
<td>已加载现有数据库模式转储</td>
</tr>
</tbody>
</table>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/migrationsmd/16699" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/mi...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/migrationsmd/16699" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/mi...</a></p>
</blockquote>
</div></template>


